import java.util.ArrayList;
import java.util.List;
//https://leetcode.cn/problems/binary-tree-level-order-traversal/submissions/555733062/
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    List<List<Integer>> list = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        levelOrderSub(root,0);
        return list;
    }

    private void levelOrderSub(TreeNode root, int level) {
        if (root == null) return;
        if (list.size() == level) {
            list.add(new ArrayList<>());
        }
        list.get(level).add(root.val);
        levelOrderSub(root.left,level+1);
        levelOrderSub(root.right,level+1);
    }
}